{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7c8a148b-736f-4c58-8db7-9d3d0fb343a6",
   "metadata": {},
   "source": [
    "# Preprocess\n",
    "## Fix the labels so that they are 1 or 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "04e0973f-ae2c-4777-963c-370c872a1d99",
   "metadata": {},
   "outputs": [],
   "source": [
    "import glob\n",
    "import nibabel as nib\n",
    "import numpy as np\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2c06f6a6-9809-4d23-87ec-175d7e3cea5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import shutil"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19ffba6d-80f9-4aae-a2b6-4940dfbc307c",
   "metadata": {},
   "source": [
    "Some of the files are not gunzip files. We can copy those to regular nifti format."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65f927cc-dd06-4c37-b7b1-3d21627b12dc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5090f475-0f84-4eaf-bc98-365653c67b0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'test.nii'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shutil.copy(\"./train/PT_84/20211229/PT_84_NB_20211229.nii.gz\", \"test.nii\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9371157f-4cb5-4796-94e9-37b93e886d33",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<nibabel.nifti1.Nifti1Image at 0x14fc4e9204d0>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image = nib.load(\"test.nii\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "befbba93-eb45-4375-9be7-ac253e9d0d00",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./train/PT_11/20181024/PT_11_NB_20181024.nii.gz --> ./preprocessed/PT_11/20181024/PT_11_NB_20181024.nii.gz\n",
      "62160\n",
      "./train/PT_11/20190717/PT_11_NB20190717.nii.gz --> ./preprocessed/PT_11/20190717/PT_11_NB20190717.nii.gz\n",
      "36182\n",
      "./train/PT_88/20190501/PT_88_NB_20190501.nii --> ./preprocessed/PT_88/20190501/PT_88_NB_20190501.nii.gz\n",
      "40746\n",
      "./train/PT_30/20190103/PT_30_NB_20190103.nii.gz --> ./preprocessed/PT_30/20190103/PT_30_NB_20190103.nii.gz\n",
      "58288\n",
      "./train/PT_30/20180903/PT_30_NB_20180903.nii.gz --> ./preprocessed/PT_30/20180903/PT_30_NB_20180903.nii.gz\n",
      "198414\n",
      "./train/PT_30/20181005/PT_30_NB_20181005.nii.gz --> ./preprocessed/PT_30/20181005/PT_30_NB_20181005.nii.gz\n",
      "157290\n",
      "./train/PT_72/20191115/PT_72_NB_20191115.nii.gz --> ./preprocessed/PT_72/20191115/PT_72_NB_20191115.nii.gz\n",
      "42646\n",
      "./train/PT_72/20190410/PT_72_NB_20190410.nii.gz --> ./preprocessed/PT_72/20190410/PT_72_NB_20190410.nii.gz\n",
      "473556\n",
      "./train/PT_72/20190522/PT_72_NB_20190522.nii.gz --> ./preprocessed/PT_72/20190522/PT_72_NB_20190522.nii.gz\n",
      "183155\n",
      "./train/PT_61/20221019/PT_61_NB_20221019.nii.gz --> ./preprocessed/PT_61/20221019/PT_61_NB_20221019.nii.gz\n",
      "4496\n",
      "./train/PT_61/20220308/PT_61_NB_20220308.nii.gz --> ./preprocessed/PT_61/20220308/PT_61_NB_20220308.nii.gz\n",
      "73233\n",
      "./train/PT_61/20220603/PT_61_NB_20220603.nii.gz --> ./preprocessed/PT_61/20220603/PT_61_NB_20220603.nii.gz\n",
      "46928\n",
      "./train/PT_16/20221007/PT_16_NB_20221007.nii.gz --> ./preprocessed/PT_16/20221007/PT_16_NB_20221007.nii.gz\n",
      "284193\n",
      "./train/PT_16/20220712/PT_16_NB_20220712.nii.gz --> ./preprocessed/PT_16/20220712/PT_16_NB_20220712.nii.gz\n",
      "243552\n",
      "./train/PT_59/20191101/PT_59_NB_20191101.nii.gz --> ./preprocessed/PT_59/20191101/PT_59_NB_20191101.nii.gz\n",
      "137986\n",
      "./train/PT_59/20200109/PT_59_NB_20200109.nii.gz --> ./preprocessed/PT_59/20200109/PT_59_NB_20200109.nii.gz\n",
      "9138\n",
      "./train/PT_24/20211019/PT_24_NB_20211019.nii.gz --> ./preprocessed/PT_24/20211019/PT_24_NB_20211019.nii.gz\n",
      "1617\n",
      "./train/PT_24/20210413/PT_24_NB_20210413.nii.gz --> ./preprocessed/PT_24/20210413/PT_24_NB_20210413.nii.gz\n",
      "93263\n",
      "./train/PT_24/20210521/PT_24_NB_20210521.nii.gz --> ./preprocessed/PT_24/20210521/PT_24_NB_20210521.nii.gz\n",
      "32560\n",
      "./train/PT_53/20220218/PT_53_NB_20220218.nii.gz --> ./preprocessed/PT_53/20220218/PT_53_NB_20220218.nii.gz\n",
      "10955\n",
      "./train/PT_53/20220401/PT_53_NB_20220401.nii.gz --> ./preprocessed/PT_53/20220401/PT_53_NB_20220401.nii.gz\n",
      "9353\n",
      "./train/PT_53/20220103/PT_53_NB_20220103.nii.gz --> ./preprocessed/PT_53/20220103/PT_53_NB_20220103.nii.gz\n",
      "159986\n",
      "./train/PT_65/20221007/PT_65_NB_20221007.nii.gz --> ./preprocessed/PT_65/20221007/PT_65_NB_20221007.nii.gz\n",
      "36551\n",
      "./train/PT_65/20220826/PT_65_NB_20220826.nii.gz --> ./preprocessed/PT_65/20220826/PT_65_NB_20220826.nii.gz\n",
      "396363\n",
      "./train/PT_81/20200515/PT_81_NB_20200515.nii.gz --> ./preprocessed/PT_81/20200515/PT_81_NB_20200515.nii.gz\n",
      "3821\n",
      "./train/PT_81/20191003/PT_81_NB_20191003.nii.gz --> ./preprocessed/PT_81/20191003/PT_81_NB_20191003.nii.gz\n",
      "10920\n",
      "./train/PT_81/20200129/PT_81_NB_20200129.nii.gz --> ./preprocessed/PT_81/20200129/PT_81_NB_20200129.nii.gz\n",
      "4112\n",
      "./train/PT_81/20190424/PT_81_NB_20190424.nii.gz --> ./preprocessed/PT_81/20190424/PT_81_NB_20190424.nii.gz\n",
      "293764\n",
      "./train/PT_39/20190522/PT_38_NB_20190522.nii.gz --> ./preprocessed/PT_39/20190522/PT_38_NB_20190522.nii.gz\n",
      "1771\n",
      "./train/PT_92/20181026/PT_92_NB_20181026.nii.gz --> ./preprocessed/PT_92/20181026/PT_92_NB_20181026.nii.gz\n",
      "16666\n",
      "./train/PT_06/20180628/PT_06_NB_20180628.nii.gz --> ./preprocessed/PT_06/20180628/PT_06_NB_20180628.nii.gz\n",
      "31306\n",
      "./train/PT_71/20190808/PT_71_NB_20190808.nii.gz --> ./preprocessed/PT_71/20190808/PT_71_NB_20190808.nii.gz\n",
      "18234\n",
      "./train/PT_49/20220610/PT_49_NB_20220610.nii.gz --> ./preprocessed/PT_49/20220610/PT_49_NB_20220610.nii.gz\n",
      "21754\n",
      "./train/PT_34/20190614/PT_34_NB_20190614.nii.gz --> ./preprocessed/PT_34/20190614/PT_34_NB_20190614.nii.gz\n",
      "32881\n",
      "./train/PT_34/20200417/PT_34_NB_20200417.nii.gz --> ./preprocessed/PT_34/20200417/PT_34_NB_20200417.nii.gz\n",
      "7327\n",
      "./train/PT_34/20190322/PT_34_NB_20190322.nii.gz --> ./preprocessed/PT_34/20190322/PT_34_NB_20190322.nii.gz\n",
      "626891\n",
      "./train/PT_34/20190501/PT_34_NB_20190501.nii.gz --> ./preprocessed/PT_34/20190501/PT_34_NB_20190501.nii.gz\n",
      "106498\n",
      "./train/PT_34/20200306/PT_34_NB_20200306.nii.gz --> ./preprocessed/PT_34/20200306/PT_34_NB_20200306.nii.gz\n",
      "2931\n",
      "./train/PT_62/20200812/PT_62_NB_20200812.nii.gz --> ./preprocessed/PT_62/20200812/PT_62_NB_20200812.nii.gz\n",
      "172885\n",
      "./train/PT_62/20200629/PT_62_NB_20200629.nii.gz --> ./preprocessed/PT_62/20200629/PT_62_NB_20200629.nii.gz\n",
      "468749\n",
      "./train/PT_62/20200925/PT_62_NB_20200925.nii.gz --> ./preprocessed/PT_62/20200925/PT_62_NB_20200925.nii.gz\n",
      "114173\n",
      "./train/PT_86/20211029/PT_86_NB_20211029.nii.gz --> ./preprocessed/PT_86/20211029/PT_86_NB_20211029.nii.gz\n",
      "8754\n",
      "./train/PT_86/20201008/PT_86_NB_20201008.nii.gz --> ./preprocessed/PT_86/20201008/PT_86_NB_20201008.nii.gz\n",
      "903537\n",
      "./train/PT_86/20210903/PT_86_NB_20210903.nii.gz --> ./preprocessed/PT_86/20210903/PT_86_NB_20210903.nii.gz\n",
      "54902\n",
      "./train/PT_27/20180824/PT_27_NB_20180824.nii.gz --> ./preprocessed/PT_27/20180824/PT_27_NB_20180824.nii.gz\n",
      "124038\n",
      "./train/PT_27/20190626/PT_27_NB_20190626.nii.gz --> ./preprocessed/PT_27/20190626/PT_27_NB_20190626.nii.gz\n",
      "5274\n",
      "./train/PT_17/20210702/PT_17_NB_20210702.nii.gz --> ./preprocessed/PT_17/20210702/PT_17_NB_20210702.nii.gz\n",
      "17428\n",
      "./train/PT_17/20210409/PT_17_NB_20210409.nii.gz --> ./preprocessed/PT_17/20210409/PT_17_NB_20210409.nii.gz\n",
      "82834\n",
      "./train/PT_17/20210526/PT_17_NB_20210526.nii.gz --> ./preprocessed/PT_17/20210526/PT_17_NB_20210526.nii.gz\n",
      "36529\n",
      "./train/PT_60/20210519/PT_60_NB_20210519.nii.gz --> ./preprocessed/PT_60/20210519/PT_60_NB_20210519.nii.gz\n",
      "87188\n",
      "./train/PT_60/20210630/PT_60_NB_20210630.nii.gz --> ./preprocessed/PT_60/20210630/PT_60_NB_20210630.nii.gz\n",
      "45700\n",
      "./train/PT_60/20210813/PT_60_NB_20210813.nii.gz --> ./preprocessed/PT_60/20210813/PT_60_NB_20210813.nii.gz\n",
      "5206\n",
      "./train/PT_84/20211229/PT_84_NB_20211229.nii.gz --> ./preprocessed/PT_84/20211229/PT_84_NB_20211229.nii.gz\n",
      "182721\n",
      "./train/PT_52/20200416/PT_52_NB_20200416.nii.gz --> ./preprocessed/PT_52/20200416/PT_52_NB_20200416.nii.gz\n",
      "2305\n",
      "./train/PT_52/20200228/PT_52_NB_20200228.nii.gz --> ./preprocessed/PT_52/20200228/PT_52_NB_20200228.nii.gz\n",
      "5002\n",
      "./train/PT_52/20201021/PT_52_NB_20201021.nii.gz --> ./preprocessed/PT_52/20201021/PT_52_NB_20201021.nii.gz\n",
      "1916\n",
      "./train/PT_58/20210908/PT_58_NB_202108.nii.gz --> ./preprocessed/PT_58/20210908/PT_58_NB_202108.nii.gz\n",
      "408062\n",
      "./train/PT_58/20211105/PT_58_NB_20211105.nii.gz --> ./preprocessed/PT_58/20211105/PT_58_NB_20211105.nii.gz\n",
      "634098\n",
      "./train/PT_10/20180815/PT_10_NB_20180815.nii.gz --> ./preprocessed/PT_10/20180815/PT_10_NB_20180815.nii.gz\n",
      "25636\n",
      "./train/PT_10/20190911/PT_10_NB_20190911.nii.gz --> ./preprocessed/PT_10/20190911/PT_10_NB_20190911.nii.gz\n",
      "2035\n",
      "./train/PT_83/20191129/PT_83_NB_20191129.nii.gz --> ./preprocessed/PT_83/20191129/PT_83_NB_20191129.nii.gz\n",
      "782211\n",
      "./train/PT_63/20190515/PT_63_NB_20190515.nii.gz --> ./preprocessed/PT_63/20190515/PT_63_NB_20190515.nii.gz\n",
      "8369\n",
      "./train/PT_63/20190404/PT_63_NB_20190404.nii.gz --> ./preprocessed/PT_63/20190404/PT_63_NB_20190404.nii.gz\n",
      "70179\n",
      "./train/PT_63/20190815/PT_63_NB_20190815.nii.gz --> ./preprocessed/PT_63/20190815/PT_63_NB_20190815.nii.gz\n",
      "4829\n",
      "./train/PT_07/20180801/PT_7_NB_20180801.gz.nii --> ./preprocessed/PT_07/20180801/PT_7_NB_20180801.nii.gz\n",
      "1158178\n",
      "./train/PT_07/20181109/PT_07_NB_201811.nii.gz --> ./preprocessed/PT_07/20181109/PT_07_NB_201811.nii.gz\n",
      "1504984\n",
      "./train/PT_38/20220408/PT_38_NB_20220408.nii.gz --> ./preprocessed/PT_38/20220408/PT_38_NB_20220408.nii.gz\n",
      "17947\n",
      "./train/PT_38/20211230/PT_38_NB_20211230.nii.gz --> ./preprocessed/PT_38/20211230/PT_38_NB_20211230.nii.gz\n",
      "16288\n",
      "./train/PT_77/20210507/PT_77_NB_20210507.nii.gz --> ./preprocessed/PT_77/20210507/PT_77_NB_20210507.nii.gz\n",
      "61348\n",
      "./train/PT_56/20211022/PT_56_NB_20211022.nii.gz --> ./preprocessed/PT_56/20211022/PT_56_NB_20211022.nii.gz\n",
      "4815\n",
      "./train/PT_56/20210618/PT_56_NB_20210618.nii.gz --> ./preprocessed/PT_56/20210618/PT_56_NB_20210618.nii.gz\n",
      "51154\n",
      "./train/PT_56/20210325/PT_56_NB_20210325.nii.gz --> ./preprocessed/PT_56/20210325/PT_56_NB_20210325.nii.gz\n",
      "558056\n",
      "./train/PT_56/20220126/PT_56_NB_2022012.nii.gz --> ./preprocessed/PT_56/20220126/PT_56_NB_2022012.nii.gz\n",
      "3576\n",
      "./train/PT_21/20210521/PT_21_NB_20210521.nii.gz --> ./preprocessed/PT_21/20210521/PT_21_NB_20210521.nii.gz\n",
      "94417\n",
      "./train/PT_80/20190910/PT_80_NB_20190910.nii.gz --> ./preprocessed/PT_80/20190910/PT_80_NB_20190910.nii.gz\n",
      "3148\n",
      "./train/PT_80/20190704/PT_80_NB_20190704.nii.gz --> ./preprocessed/PT_80/20190704/PT_80_NB_20190704.nii.gz\n",
      "80762\n",
      "./train/PT_80/20190410/PT_80_NB_20190410.nii.gz --> ./preprocessed/PT_80/20190410/PT_80_NB_20190410.nii.gz\n",
      "699354\n"
     ]
    }
   ],
   "source": [
    "out_dir = \"./preprocessed\"\n",
    "os.makedirs(out_dir, exist_ok=True)\n",
    "for filename in glob.glob(\"./train/*/*/*.nii*\"):\n",
    "    if \"NB\" in filename:\n",
    "\n",
    "        try:\n",
    "            image = nib.load(filename)\n",
    "        except nib.filebasedimages.ImageFileError:\n",
    "            # Some of the files are not gunzip files. We can copy those to regular nifti format.\n",
    "            shutil.copy(filename, \"tmp.nii\")\n",
    "            image = nib.load(\"tmp.nii\")\n",
    "        data = np.asarray(image.dataobj)\n",
    "        fixed = np.asarray(data > 0, data.dtype)\n",
    "        fixed_image = image.__class__(fixed, affine=image.affine)\n",
    "        _out_dir = os.path.join(out_dir, *filename.split(\"/\")[-3:-1])\n",
    "        out_filename = os.path.join(_out_dir, os.path.basename(filename).split(\".\")[0]) + \".nii.gz\"\n",
    "        os.makedirs(_out_dir, exist_ok=True)\n",
    "        #assert not os.path.exists(out_filename)\n",
    "        print(filename, \"-->\", out_filename)\n",
    "        #fixed_image.to_filename(out_filename)\n",
    "        print(fixed.sum())\n",
    "        if os.path.exists(\"tmp.nii\"):\n",
    "            os.remove(\"tmp.nii\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f84e36cd-7e85-4298-b79f-15c64ebe00d4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (brats)",
   "language": "python",
   "name": "brats"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
